/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.pbv.azsg.web;

import java.util.List;
import java.io.IOException;

import com.pbv.azsg.domain.Dispenser;
import com.pbv.azsg.service.IDispenserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.http.HttpStatus;
import org.springframework.ui.Model;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;    

/**
 *
 * @author b.prikhodko
 */
@Controller
public class DispenserController {
    
    @Autowired
    private IDispenserService dispenserSevice;
    
    @RequestMapping(value = "/dispenser/get")
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Dispenser getDispenser(Long id) {            
        return dispenserSevice.getDispenser( id );                      
    }

    @RequestMapping(value = "/dispenser/update")
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Dispenser updateDispenser(String json) {    
        ObjectMapper jsonmapper = new ObjectMapper();
        try {
          return dispenserSevice.updateDispenser( jsonmapper.readValue(json, Dispenser.class) );                      
        } catch ( IOException e ) {
            System.out.println(e);
            return new Dispenser();
        }

    }
}
